﻿function PostLazzyLoader() {

    this.TempDiv = $('<div />');
    this.LoadUrl = location.href;

    var c = '?';
    if (this.LoadUrl.indexOf('?') > 0)
        c = '&';
    this.LoadUrl = this.LoadUrl + c + 'lastId={0} #nemoBlog';


    this.ButtonTrigger = $('*[data-action=load-more-posts]');
    this.ButtonTrigger.css('cursor', 'pointer').click(function (e) {
        e.preventDefault();
        window.PostLoader.LoadMorePosts();
    });

    this.GetLastPostId = function () {
        return $('*[data-postid]:last').attr('data-postid');
    }

    this.LoadMorePosts = function () {
        this.TempDiv = $('<div />');
        var url = this.LoadUrl.replace('{0}', this.GetLastPostId());
        this.TempDiv.load(url, function () {
            window.PostLoader.__LoadComplete();
        });
    }

    this.__LoadComplete = function () {
        var haveElements = this.TempDiv.find('*[data-postid]').length > 0;
        if (haveElements)
            this.TempDiv.find('*[data-postid]').appendTo($('#nemoBlog'));
        else
            this.ButtonTrigger.hide();
    }
}

$(function () {
    window.PostLoader = new PostLazzyLoader();
    $('*[data-action=load-more-posts]').css('cursor', 'pointer').click(function (e) {
        e.preventDefault();
        window.PostLoader.LoadMorePosts();
    });
});